<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\common\service\SysLogService;


class Articletask extends Controller
{
    public function getTaskList()
    {

        $data = request()->post();
        $page = isset($data['page']) ? intval($data['page']) : 1;
        $limit = isset($data['limit']) ? intval($data['limit']) : 10;
        
        $map = [];
        $map['is_delete'] = ['=', '0'];
        
        // 如果有搜索条件，可以在这里添加
        if (isset($data['name']) && !empty($data['name'])) {
            $map['name'] = ['like', '%' . $data['name'] . '%'];
        }
        
        if (isset($data['qzh']) && !empty($data['qzh'])) {
            $map['qzh'] = ['=', $data['qzh']];
        }
        
        if (isset($data['status']) && $data['status'] !== '') {
            $map['status'] = ['=', intval($data['status'])];
        } 
        
        // 获取总数
        $count = Db::table('zstp_article_task')
            ->where($map)
            ->count();
            
        // 获取分页数据
        $list = Db::table('zstp_article_task')
            ->where($map)
            ->page($page, $limit)
            ->order('create_time desc')
            ->select();


        $path= config('path');
        foreach($list as $key => $value){
            //查他的全宗号
            $qzh = "";
            $qzhResult = Db::table('zstp_article_ocr')->field('qzh')->where('task_id', $value['id'])->find();
         
            if(!empty($qzhResult)){
                $qzh = $qzhResult['qzh'];
            }
            $list[$key]['qzh'] = $qzh;
            $pathName = $path  . "pdfpackaging/" . $value['id'] . "/{$qzh}/";
            $list[$key]['pathName'] = $pathName;
           //查进度
           $mapResult['task_id'] = $value['id'];
           $progress = Db::table('zstp_article_ocr')->where($mapResult)->count();

           $mapResult['ocr_status'] = 1;
           $countResult = Db::table('zstp_article_ocr')
           ->where($mapResult)
           ->count();

           if($progress > 0){
            $progress = $countResult/$progress*100;
           }else{
            $progress = 0;
           }
           
           $list[$key]['progress'] = round($progress,2);
        }
        

     
            
        return json([
            'code' => 200,
            'message' => '获取成功',
            'data' => [
                'total' => $count,
                'items' => $list,
                'page' => $page,
                'limit' => $limit
            ]
        ]);

        
       

    }

    public function batchdeletetasklist()
    {
        $data = request()->post();
        $ids = isset($data['ids']) ? $data['ids'] : '';

        if (empty($ids)) {
            return json(['code' => 400, 'message' => '参数错误']);
        }

        $result = Db::table('zstp_article_task')->where('id', 'in', $ids)->update(['is_delete' => '1']);

        Db::table('zstp_article_ocr')->where('task_id', 'in', $ids)->update(['is_delete' => '1']);


        if ($result) {

            //日志
            $sysLogService = new SysLogService();
            $result =  $sysLogService->addLog('批量删除批处理任务('.$ids.')', $ids, 3, '任务id:'.$ids);

            return json(['code' => 200, 'message' => '批量删除成功']);
        } else {
            return json(['code' => 500, 'message' => '批量删除失败']);
        }
    }

    public function deletetasklist()
    {
        $data = request()->post();
        $id = isset($data['id']) ? $data['id'] : '';
        
        if (empty($id)) {
            return json(['code' => 400, 'message' => '参数错误']);
        }

        $result = Db::table('zstp_article_task')->where('id', $id)->update(['is_delete' => '1']);
        Db::table('zstp_article_ocr')->where('task_id', $id)->update(['is_delete' => '1']);

        if ($result) {
            //日志
            $sysLogService = new SysLogService();
            $result =  $sysLogService->addLog('删除批处理任务('.$id.')', $id, 3, '任务id:'.$id);

            return json(['code' => 200, 'message' => '删除成功']);
        } else {
            return json(['code' => 500, 'message' => '删除失败']);
        }
        
    }


    public function retryfaileditems(){
        $data = request()->post();
        $id = isset($data['id']) ? $data['id'] : '';

        if (empty($id)) {
            return json(['code' => 400, 'message' => '参数错误']);
        }


        $map['ocr_status'] = array("<>", 1);
        $map['task_id'] = array("=", $id);

        $result = Db::table('zstp_article_ocr')->where('task_id', $id)->where( $map)->update(['ocr_status' => '0']);

        return json(['code' => 200, 'message' => '重试成功']);

       
    }
    
}